using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[NetworkSettings(channel = 0, sendInterval = 1f)]
public class EnvironmentManager : NetworkBehaviour
{
	public DayNightCycle dayNight;

	public TreesManager Trees;

	[SyncVar]
	public float dayTimeSync;

	[SyncVar(hook = "OnTreesChanged")]
	public string TreesCutData;

	public float NetworkdayTimeSync
	{
		get
		{
			return dayTimeSync;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref dayTimeSync, 1u);
		}
	}

	public string NetworkTreesCutData
	{
		get
		{
			return TreesCutData;
		}
		[param: In]
		set
		{
			ref string treesCutData = ref TreesCutData;
			if (NetworkServer.localClientActive && !base.syncVarHookGuard)
			{
				base.syncVarHookGuard = true;
				OnTreesChanged(value);
				base.syncVarHookGuard = false;
			}
			SetSyncVar(value, ref treesCutData, 2u);
		}
	}

	public override void OnStartLocalPlayer()
	{
		if (Trees != null)
		{
			Trees.OnClientStart();
		}
		base.OnStartLocalPlayer();
	}

	private void OnTreesChanged(string treecutdata)
	{
		NetworkTreesCutData = treecutdata;
		if (Trees != null)
		{
			Trees.UpdateRemovedTrees(treecutdata);
		}
	}

	public void UpdateTrees(string treecutdata)
	{
		if (base.isServer)
		{
			NetworkTreesCutData = treecutdata;
		}
	}

	private void Start()
	{
		Trees = (TreesManager)Object.FindObjectOfType(typeof(TreesManager));
		if (dayNight == null)
		{
			dayNight = (DayNightCycle)Object.FindObjectOfType(typeof(DayNightCycle));
		}
	}

	private void Update()
	{
		if ((bool)dayNight)
		{
			if (base.isServer)
			{
				NetworkdayTimeSync = dayNight.Timer;
			}
			dayNight.Timer = dayTimeSync;
		}
	}

	private void UNetVersion()
	{
	}

	public override float GetNetworkSendInterval()
	{
		return 1f;
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(dayTimeSync);
			writer.Write(TreesCutData);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(dayTimeSync);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(TreesCutData);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			dayTimeSync = reader.ReadSingle();
			TreesCutData = reader.ReadString();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			dayTimeSync = reader.ReadSingle();
		}
		if ((num & 2) != 0)
		{
			OnTreesChanged(reader.ReadString());
		}
	}
}
